<?php
/**
 * 葫芦家首页
 * @author flaravel
 */
namespace App\Http\Controllers\V1;
use App\Http\Resources\SpikeResource;
use App\Models\Product;
use App\Models\Scope\OnSaleScope;
use App\Service\BannerService;
use App\Service\CartService;
use App\Service\ProductService;
use App\Http\Controllers\Controller;

class HomeController extends Controller
{

    /**
     * 首页
     *
     * @param BannerService $bannerService
     * @param ProductService $productService
     * @param CartService $cartService
     * @return mixed
     */
    public function index(BannerService $bannerService, ProductService $productService, CartService $cartService)
    {
        //获取首页轮播图
        $banners = $bannerService->getBanners();

        $spike = $productService->getHomeSpikeProducts();
        //获取秒杀产品
        if ($spike) {
            $spike = new SpikeResource($spike);
        }

        //获取二级分类
        $cate = $productService->getProductTwoCate();

        //购物车数量
        $cartCount = $cartService->count();

        return $this->success([
            'banners'   => $banners,
            'seckill'   => $spike ?? '',
            'cate'      => $cate,
            'ad'      => $this->getHomeAd(),
            'cartCount' => $cartCount
        ]);
    }

    // 获取首页的广告位
    public function getHomeAd()
    {
        return [
            "name" => "领取优惠券广告位",
            "url" => "https://img.hulujia119.com/banner/db99f44930cf4a8b8ec28e32a1387c6b.png",
            "data" =>  [
                'type' => 1,  // 1 跳转url  jump_url , 2 跳转商品 product_id
                "jump_url" => 'https://h5.hulujia119.com/coupon'
            ]
        ];
    }
}
